/*
 * hardware.cpp
 *
 *  Created on: 2024年11月27日
 *      Author: ZKSWE Develop Team
 */

#include <base/handler.hpp>
#include "hw/HardwareManager.h"
#include "utils/GpioHelper.h"
#include "hardware.h"
#include "common.h"
#include "logic/global_variables.h"

namespace hw {

#define BEEP_IO    "GPIO_5"

enum {
	E_MSG_BEEP,
};

static base::handler<int> handler_;

static void _message_handler(const base::message<int> *msg, const void *user_data) {
	if (msg->what == E_MSG_BEEP) {
		GpioHelper::output(BEEP_IO, 1);
		DELAY(msg->obj);
		GpioHelper::output(BEEP_IO, 0);
	}
}

void init() {
	// 自定义按键音
	HARDWAREMANAGER->setCustomBeep([] { beep(); });

	handler_.set_message_handler(_message_handler, NULL);
}

void beep(int time) {
	if(Global::Flags::TouchSound){
		base::message<int> msg(E_MSG_BEEP, time);
		handler_.send_message(msg);
	}
}

}
